/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package videostoreapp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import videostoreapp.Order.OrderInfo;
import videostoreapp.Order.OrderLineInfo;
import videostoreapp.dataobjects.OrderDO;
/**
 *
 * @author pmae9287
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {

            Reader inputStreamReader = new InputStreamReader(System.in);
            Order o=new OrderDO();

            String s="";
            while (!s.equals("4")) {
                try {
                    
                        System.out.println("*********************************************");
                        System.out.println("************* Video Store Menu **************");
                        System.out.println("*********************************************");
                        System.out.println("");
                        System.out.println("Please, choose one of the following options :");
                        System.out.println("");
                        System.out.println("1- Submit a New Order");
                        System.out.println("2- Check Order Status"); // Ask for order_id
                        System.out.println("3- Send Status request of an order to the warehouse"); // Ask for order_id
                        System.out.println("4- Quit the System");
                        System.out.println("");
                        System.out.println("Write your option and press Enter key : ");
                        System.out.println("");
                    
                        BufferedReader answer = new BufferedReader(new InputStreamReader(System.in));
                        s = answer.readLine();

                        if (s.equals("1")) {
                            System.out.println("");
                            System.out.println("Submitting a New Order");
                            System.out.println("");
                            submitOrder();
                        } else if (s.equals("2")) {
                            System.out.println("");
                            System.out.println("Check Order Order Status");
                            System.out.println("Please enter the Order ID: ");
                            BufferedReader idRead = new BufferedReader(new InputStreamReader(System.in));
                            String id=idRead.readLine();
                            Integer idInt=new Integer(id);
                            OrderInfo oi=o.getOrderById(idInt);
                            System.out.println("Order "+oi.id+": Status "+oi.status);
                        } else if (s.equals("3")) {
                            System.out.println("");
                            System.out.println(" Status oooooooo request of an order to the warehouse");
                            System.out.println("Please enter the Order ID: ");
                            BufferedReader idRead = new BufferedReader(new InputStreamReader(System.in));
                            String id=idRead.readLine();
                            Integer idInt=new Integer(id);
                            checkOrderStatus(idInt);
                            System.out.println("");
                        }  else if (s.equals("4")) {
                            System.out.println("");
                            System.out.println("Quit the System");
                            System.out.println("");
                            OrderMgr.getInstance().stopQueueListener();
                            System.exit(0);
                        } else{
                            System.out.println("");
                            System.out.println("Invalid option. Please, select one of listed options.");
                            System.out.println("");
                        }

                } catch (IOException e) {
                    System.err.println("I/O exception: " + e.toString());
                }
            }

    }

    public static void submitOrder() throws InterruptedException {

        Order order = new OrderDO();
        OrderInfo ord = new OrderInfo(0, 2, 0.0f, new Timestamp(Calendar.getInstance().getTimeInMillis()), "Sydney", "Pitt St", OrderInfo.OrderStatus.PENDING);
        OrderLineInfo[] ol = new OrderLineInfo[2];
        ol[0] = new OrderLineInfo(0, 0, 2, 1);
        ol[1] = new OrderLineInfo(0, 0, 1, 1);
        ord.videos = ol;
        try {
            int oId1 = order.createOrder(ord);

            System.out.println("New order created with Id " + oId1);

            Thread.sleep(1000);

            order.submitOrder(oId1);

            System.out.println("Order " + oId1 + " submitted to warehouse queue");
        } catch (ApplicationException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public static void checkOrderStatus(int orderID) throws Exception {

        System.out.println("order "+orderID);
        OrderMgr.getInstance().sendDeliveryQuery(orderID);

    }

}
